package linkedlist;

import java.util.Stack;

/**
 * @author 十八岁讨厌编程
 * @date 2025/6/27 21:12
 * @PROJECT_NAME leetcode
 * @description
 */

public class Solution0207 {


    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        if (headA == null || headB == null) return null;
        ListNode pointerA = headA;
        ListNode pointerB = headB;
        Stack<ListNode> a = new Stack<ListNode>();
        Stack<ListNode> b = new Stack<ListNode>();
        while (pointerA != null) {
            a.push(pointerA);
            pointerA = pointerA.next;
        }
        while (pointerB != null) {
            b.push(pointerB);
            pointerB = pointerB.next;
        }
        ListNode nodeA = a.pop();
        ListNode nodeB = b.pop();
        ListNode result = null;
        while (nodeA == nodeB) {
            result = nodeA;
            if (a.empty() || b.empty()) break;
            nodeA = a.pop();
            nodeB = b.pop();
        }
        return result;
    }


}
